package com.hnkywl.sts.service.jsgl;

import com.hnkywl.sts.dao.jsgl.XsjsdDao;
import com.hnkywl.sts.dao.jsgl.XsjsdmxDao;
import com.hnkywl.sts.dao.sfgl.SkshDao;
import com.hnkywl.sts.dao.xsgl.JfmxDao;
import com.hnkywl.sts.dao.xsgl.XsdDao;
import com.hnkywl.sts.dao.xsgl.XsmxDao;
import com.hnkywl.sts.dao.zjgl.HthkDao;
import com.hnkywl.sts.entity.cggl.Jsd;
import com.hnkywl.sts.entity.cggl.Xsjsdmx;
import com.hnkywl.sts.entity.htgl.Ht;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.sys.Gg;
import com.hnkywl.sts.entity.xsgl.Jfmx;
import com.hnkywl.sts.entity.xsgl.Xsd;
import com.hnkywl.sts.entity.xsgl.Xsmx;
import com.hnkywl.sts.entity.zjgl.Hthk;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.xsgl.XsdService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.CalcUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-23
 * Time: 上午10:55
 * To change this template use File | Settings | File Templates.
 */
@Service
public class XsjsdService {
    private XsjsdDao xsjsdDao;
    private XsjsdmxDao xsjsdmxDao;
    private XsdDao xsdDao;
    private BmService bmService;
    private XsmxDao xsmxDao;
    private XsdService xsdService;
    private JfmxDao jfmxDao;
    private SkshDao skshDao;
    private HthkDao hthkDao;

    @Transactional
    public String findAllXsjsd(Integer zt, String jsdh, Date sqrqStart, Date sqrqEnd, Long glqyId, String glqyQc, Long gsId, String gsJc, Long fpzlId, int pageNo, int pageSize) {
        if ("".equals(gsJc)) {
            gsId = null;
        }
        Page<Jsd> xsjsdPage = this.xsjsdDao.findXsjsdList(zt, jsdh, sqrqStart, sqrqEnd, glqyId, glqyQc, gsId, gsJc, fpzlId, pageNo, pageSize);
        List<Jsd> xsjsdList = xsjsdPage.getResult() == null ? new ArrayList<Jsd>() : xsjsdPage.getResult();
        String xsjsdJson = BaseUtils.fetchCollectionToJson(xsjsdList, "id", "jsdh", "sqrq", "type", "glqy.id", "glqy.qc", "glqy.qyfr.nsh", "fpze", "ddfy", "fpzl", "hm", "sqr.id", "sqr.xm", "gs.id", "gs.jc", "cggs.id", "cggs.jc", "zt", "fpthyy", "sftzd");
        return "{\"total\":" + xsjsdPage.getTotalCount() + ",\"rows\":[" + xsjsdJson + "]}";
    }

    /**
     * 首页显示销售结算单
     *
     * @return
     */
    public String findAllXsjsd(Integer zt, Integer type) {
        return this.xsjsdDao.findAllXsjsd(zt, type);
    }

    /**
     * 保存销售单结算
     *
     * @param xsjsd
     */
    @Transactional
    public void saveXsjsd(Jsd xsjsd) throws SQLException {
        xsjsd.setZt(0);
        xsjsd.setShzt(0);
        xsjsd.setCggs(xsjsd.getGs());
        nullId(xsjsd);
        xsjsd.setSqrq(new Date());
        xsjsd.setType(1); //type=0 采购 ; 1 销售，2物流
        xsjsd.setDateCreated(new Date());
        xsjsd.setLastUpdated(xsjsd.getDateCreated());
        xsjsd.setCreateUser(YgService.getCurrentYg());
        xsjsd.setCreateOrgan(YgService.getCurrentYg().getZggs());
        xsjsd.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        xsjsd.setJsdh(this.bmService.getJsdbm("XSJS"));
        this.xsjsdDao.save(xsjsd);
        if (xsjsd.getXsdList() != null) {
            for (Xsd x : xsjsd.getXsdList()) {
                Xsd newInstance = xsdDao.findUniqueByProperty("id", x.getId());
                newInstance.setXsjsd(xsjsd);
                newInstance.setLastUpdated(new Date());
                this.xsdDao.save(newInstance);

                List<Jfmx> jfmxList = this.jfmxDao.findJfmxByXsdId(newInstance.getId());
                for (Jfmx jfmx : jfmxList) {
                    jfmx.setSfjs(true);
                    this.jfmxDao.save(jfmx);
                }
            }
        }
        if (xsjsd.getXsdmxList() != null) {
            for (Xsmx temp : xsjsd.getXsdmxList()) {
                Xsmx clone = xsmxDao.findUniqueByProperty("id", temp.getId());
                clone.setJsbhse(CalcUtils.round(temp.getJsbhse(), 2));
                clone.setJsse(CalcUtils.round(temp.getJsse(), 2));
                clone.setDlf(CalcUtils.round(temp.getDlf(), 2));
                clone.setZjzyf(CalcUtils.round(temp.getZjzyf(), 2));
                clone.setSjghje(CalcUtils.round(temp.getSjghje(), 2));
                clone.setTxx(CalcUtils.round(temp.getTxx(), 2));
                clone.setSe(CalcUtils.round(temp.getJsse() - temp.getJsbhse(), 2));
                clone.setXkyh(temp.getXkyh());
                clone.setKpbhsj(CalcUtils.round(temp.getKpbhsj(), 6));
                clone.setLastUpdated(new Date());
                clone.setCreateUser(YgService.getCurrentYg());
                clone.setCreateOrgan(YgService.getCurrentYg().getZggs());
                clone.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                this.xsmxDao.save(clone);
            }

            Xsd xsd = this.xsdDao.get(xsjsd.getXsdList().get(0).getId());
            // 按比例释放保证金情况
            if (xsd.getXsht().getZddhcght().getBzjfs() == 1) {
                String xsdIds = "";
                if (xsjsd.getXsdmxList().get(0).getTxx() > 0) {
                    for (Xsd xsdTemp : xsjsd.getXsdList()) {
                        xsdIds += xsdTemp.getId() + ",";
                    }
                    List<Map> listMap = this.skshDao.findAllSfkByKh(xsdIds.substring(0, xsdIds.length() - 1), null);
                    if (null != listMap && listMap.size() > 0) {
                        for (Map map : listMap) {
                            Sfk sfk = this.skshDao.get(Long.valueOf(map.get("ID").toString()));
                            sfk.setTxye(0.0);
                            sfk.setJsd(xsjsd);
                            this.skshDao.save(sfk);
                        }
                    }
                }
            }
            // 无保证金结算,把合同关联贴息使用完
            if (xsd.getXsht().getZddhcght().getBzjfs() == 2) {
                if (xsjsd.getXsdmxList().get(0).getTxx() > 0) {
                    List<Hthk> hthkList = this.hthkDao.findAllByCght(xsd.getXsht().getZddhcght(), null);
                    if (null != hthkList && hthkList.size() > 0) {
                        for (Hthk hthk : hthkList) {
                            hthk.setTxye(0.0);
                            hthk.setJsd(xsjsd);
                            this.hthkDao.save(hthk);
                        }
                    }
                }
            }

            // 一次性释放保证金情况
            if (xsd.getXsht().getZddhcght().getBzjfs() == 0) {
                if (xsjsd.getXsdmxList().get(0).getTxx() > 0 || xsjsd.getXsdmxList().get(0).getDlf() > 0) {
                    List<Jfmx> jfmxList = this.jfmxDao.findJfmxByXshtId(xsd.getXsht().getId(), null);
                    if (null != jfmxList && jfmxList.size() > 0) {
                        for (Jfmx jfmx : jfmxList) {
                            jfmx.setSfjs(true);
                            jfmx.setJsd(xsjsd);
                            this.jfmxDao.save(jfmx);
                        }
                    }
                }
            }
        }
    }

    /**
     * 修改销售单结算
     *
     * @param xsjsd
     */
    @Transactional
    public void updateXsjsd(Jsd xsjsd) {
        Jsd clone = this.findXsjsdDetail(xsjsd.getId());
        xsjsd.setSqrq(clone.getSqrq());
        xsjsd.setZt(clone.getZt());
        xsjsd.setShzt(clone.getShzt());
//        xsjsd.setKprq(clone.getKprq());
        xsjsd.setKpr(clone.getKpr());
        nullId(xsjsd);
        clone = (Jsd) ReflectionUtils.getInstance(clone, xsjsd);
        clone.setCggs(clone.getGs());
        clone.setLastUpdated(new Date());
        this.xsjsdDao.save(clone);
        if (xsjsd.getXsdList() != null) {
            for (Xsd x : xsjsd.getXsdList()) {
                Xsd newInstance = xsdDao.findUniqueByProperty("id", x.getId());
                newInstance.setXsjsd(xsjsd);
                newInstance.setLastUpdated(new Date());
            }
        }
        if (xsjsd.getXsdmxList() != null) {
            for (Xsmx temp : xsjsd.getXsdmxList()) {
                Xsmx xs = xsmxDao.findUniqueByProperty("id", temp.getId());
                xs.setJsbhse(temp.getJsbhse());
                xs.setJsse(temp.getJsse());
                xs.setLastUpdated(new Date());
                xs.setDlf(temp.getDlf());
                xs.setZjzyf(temp.getZjzyf());
                xs.setSjghje(temp.getSjghje());
                xs.setTxx(temp.getTxx());
                xs.setXkyh(temp.getXkyh());
                xs.setKpbhsj(temp.getKpbhsj());
            }
        }
    }

    /**
     * 删除销售单结算
     *
     * @param id
     */
    @Transactional
    public void deleteXsjsd(Long id) {
        Jsd xsjsd = this.findXsjsdDetail(id);
        xsjsd.setLastUpdated(new Date());
        xsjsd.setDeleted(1);
        this.xsjsdDao.save(xsjsd);
        if (xsjsd.getXsdList() != null) {
            for (Xsd x : xsjsd.getXsdList()) {
                Xsd newInstance = xsdDao.findUniqueByProperty("id", x.getId());
                newInstance.setYjs(false);
                newInstance.setXsjsd(null);
                newInstance.setLastUpdated(new Date());
                this.xsdDao.save(newInstance);
                List<Jfmx> jfmxList = this.jfmxDao.findJfmxByXsdId(newInstance.getId());
                for (Jfmx jfmx : jfmxList) {
                    jfmx.setSfjs(false);
                    this.jfmxDao.save(jfmx);
                }
            }
        }
        if (xsjsd.getXsdmxList() != null) {
            for (Xsmx x : xsjsd.getXsdmxList()) {
                Xsmx newInstance = xsmxDao.get(x.getId());
                newInstance.setYjs(false);
                newInstance.setXsjsd(null);
                newInstance.setLastUpdated(new Date());
                this.xsmxDao.save(newInstance);
            }
        }

        // 按比例释放保证金情况
        if (xsjsd.getXsdList().get(0).getXsht().getZddhcght().getBzjfs() == 1) {
            if (xsjsd.getXsdmxList().get(0).getTxx() > 0) {
                String xsdIds = "";
                if (xsjsd.getXsdmxList().get(0).getTxx() > 0) {
                    for (Xsd xsdTemp : xsjsd.getXsdList()) {
                        xsdIds += xsdTemp.getId() + ",";
                    }
                    List<Map> listMap = this.skshDao.findAllSfkByKh(xsdIds.substring(0, xsdIds.length() - 1), xsjsd.getId());
                    if (null != listMap && listMap.size() > 0) {
                        for (Map map : listMap) {
                            Sfk sfk = this.skshDao.get(Long.valueOf(map.get("ID").toString()));
                            sfk.setTxye(sfk.getTx());
                            sfk.setJsd(null);
                            this.skshDao.save(sfk);
                        }
                    }
                }
            }
        }

        // 无保证金结算，判断是否有贴息
        if (xsjsd.getXsdList().get(0).getXsht().getZddhcght().getBzjfs() == 2) {
            if (xsjsd.getXsdmxList().get(0).getTxx() > 0) {
                List<Hthk> hthkList = this.hthkDao.findAllByCght(xsjsd.getXsdList().get(0).getXsht().getZddhcght(), xsjsd);
                if (null != hthkList && hthkList.size() > 0) {
                    for (Hthk hthk : hthkList) {
                        hthk.setTxye(hthk.getTx());
                        hthk.setJsd(null);
                        this.hthkDao.save(hthk);
                    }
                }
            }
        }

        // 一次性释放保证金
        if (xsjsd.getXsdList().get(0).getXsht().getZddhcght().getBzjfs() == 0) {
            if (xsjsd.getXsdmxList().get(0).getTxx() > 0 || xsjsd.getXsdmxList().get(0).getDlf() > 0) {
                List<Jfmx> jfmxList = this.jfmxDao.findJfmxByXshtId(xsjsd.getXsdList().get(0).getXsht().getId(), xsjsd.getId());
                if (null != jfmxList && jfmxList.size() > 0) {
                    for (Jfmx jfmx : jfmxList) {
                        jfmx.setSfjs(false);
                        jfmx.setJsd(null);
                        this.jfmxDao.save(jfmx);
                    }
                }
            }
        }
    }

    /**
     * 查询销售结算单明细
     *
     * @param id
     * @return
     */
    @Transactional
    public Jsd findXsjsdDetail(Long id) {
        return this.xsjsdDao.get(id);
    }

    /**
     * 审核状态
     *
     * @param id
     * @param status
     */
    @Transactional
    public void changeStatus(Long id, Integer status) throws Exception {
        Jsd xsjsd = this.findXsjsdDetail(id);
        xsjsd.setZt(status);
        xsjsd.setShzt(0);
        xsjsd.setLastUpdated(new Date());
        Map<Long, Xsd> map = new HashMap<Long, Xsd>();
        for (Xsmx x : xsjsd.getXsdmxList()) {
            x.setYjs(true);
            x.setLastUpdated(new Date());
            if (map.get(x.getXsd().getId()) == null)
                map.put(x.getXsd().getId(), x.getXsd());
        }
        this.xsjsdDao.save(xsjsd);

        // 保存销售结算单明细，先删除后保存
        List<Xsjsdmx> xsjsdmxList = xsjsd.getXsjsdmxList();
        if (null != xsjsdmxList && xsjsdmxList.size() > 0) {
            for (Xsjsdmx xsjsdmx : xsjsdmxList) {
                xsjsdmx.setLastUpdated(new Date());
                xsjsdmx.setDeleted(1);
                this.xsjsdmxDao.save(xsjsdmx);
            }
        }
        if (null != xsjsd && null != xsjsd.getXsdmxList()) {
            List<Xsmx> xsmxList = xsjsd.getXsdmxList();
            for (Xsmx xsmx : xsmxList) {
                Xsjsdmx xsjsdmx = new Xsjsdmx();
                xsjsdmx.setGg(xsmx.getJtwz());
                xsjsdmx.setKh(xsmx.getKh());
                xsjsdmx.setCk(xsmx.getCk());
                xsjsdmx.setBhsj(xsmx.getBhsj());
                xsjsdmx.setHsj(xsmx.getHsj());
                xsjsdmx.setCksl(xsmx.getCk_sbsl());
                xsjsdmx.setJe(xsmx.getJsbhse());
                xsjsdmx.setSe(xsmx.getSe());
                xsjsdmx.setJsd(xsjsd);
                xsjsdmx.setXsmx(xsmx);
                xsjsdmx.setXsd(xsmx.getXsd());
                xsjsdmx.setCreateUser(YgService.getCurrentYg());
                xsjsdmx.setCreateOrgan(YgService.getCurrentYg().getZggs());
                xsjsdmx.setCreateDepartment(YgService.getCurrentYg().getZgjg());
                xsjsdmx.setDateCreated(new Date());
                xsjsdmx.setLastUpdated(xsjsdmx.getDateCreated());
                this.xsjsdmxDao.save(xsjsdmx);
            }
        }

        for (Long xsdId : map.keySet()) {
            Xsd xsd = map.get(xsdId);
            List<Xsmx> xsmxList = xsd.getXsmxList();
            Boolean flag = true;
            for (Xsmx xsmx : xsmxList) {
                if (xsmx.getYjs() == null || !xsmx.getYjs()) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                xsd.setYjs(true);
                this.xsdDao.save(xsd);
            }
        }

        //   parseDlf(xsjsd);//扣除代理费用
        changeJfmxSfjs(xsjsd);//更改计费明细是否结算状态
    }

//    public void parseDlf(Jsd jsd) throws Exception {
//        java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
//
//        //先判断这次结算要缴纳的代理费用合计多少
//        List<Xsmx> xsmxes = jsd.getXsdmxList();
//        Double dlfhj = 0d;
//        for (Xsmx xsmx : xsmxes) {
//            if (xsmx.getDlf() != null && xsmx.getDlf() != 0) {
//                dlfhj += xsmx.getDlf();
//            }
//        }
//        //查询这个客户的收款中有没有款项使用为代理费用的收款
//        List<Sfk> dlfSkList = skshDao.findAllDlfSkList(jsd.getGlqy().getId());
//        //如果有
//        if (dlfSkList.size() > 0) {
//            //判断是否足够付这次结算的代理费
//            Double skye = 0d;
//            for (Sfk s : dlfSkList) {
//                if (s.getYe() > 0) {
//                    skye += s.getYe();
//                }
//            }
//            if (skye >= dlfhj) {
//                //收的款够付代理费，扣除这笔收款的代理费
//                minusSk(dlfSkList, dlfhj);
//            } else {
//                //收款的代理费用不够，需要从货款里扣
//                Double hkfy = dlfhj - skye;
//                List<Sfk> hkSkList = skshDao.findAllHkSkList(jsd.getGlqy().getId());
//                Double hkskhj = 0d;
//                for (Sfk s1 : hkSkList) {
//                    hkskhj += s1.getYe();
//                }
//                if (hkfy < hkskhj) {
//                    throw new RuntimeException("缴纳的代理费用不足！所余货款不足，请缴纳" + df.format(hkfy) + "元代理费");
//                } else {
//                    minusSk(dlfSkList, skye);
//                    minusSk(hkSkList, hkfy);
//                }
//            }
//        } else {
//            //找货款集合
//            List<Sfk> sList = skshDao.findAllHkSkList(jsd.getGlqy().getId());
//            Double h1 = 0d;
//            for (Sfk s3 : sList) {
//                if (s3.getYe() > 0) {
//                    h1 += s3.getYe();
//                }
//            }
//            if (h1 < dlfhj) {
//                throw new RuntimeException("该客户没有缴纳代理费用，其所余货款不足，请充值" + dlfhj + "元代理费");
//            } else {
//                minusSk(sList, dlfhj);
//            }
//        }
//    }

//    public void minusSk(List<Sfk> sks, Double dlfhj) {
//        for (Sfk sfk : sks) {
//            if (dlfhj > 0) {
//                if (sfk.getYe() >= dlfhj) {
//                    sfk.setYe(sfk.getYe() - dlfhj);
//                    skshDao.save(sfk);
//                }
//                if (sfk.getYe() < dlfhj) {
//                    dlfhj = dlfhj - sfk.getYe();
//                    sfk.setYe(0d);
//                    skshDao.save(sfk);
//                }
//            }
//        }
//    }

    public void changeJfmxSfjs(Jsd jsd) {
        for (Xsmx x : jsd.getXsdmxList()) {
            this.jfmxDao.changeSfjsByJfmx(x.getId());
        }
    }

    /**
     * 销售结算单冲销
     *
     * @param yxsjsd
     */
    @Transactional
    public void xsjsdtz(Jsd yxsjsd) {
        Jsd hcxsjsd = new Jsd();
        ReflectionUtils.getInstance(hcxsjsd, yxsjsd);
        nullId(hcxsjsd);
        hcxsjsd.setId(null);
        hcxsjsd.setJsdh(this.bmService.getJsdbm("XSJS"));
        if (hcxsjsd.getDdfy() != null)
            hcxsjsd.setDdfy(0 - hcxsjsd.getDdfy());
        hcxsjsd.setMe(0 - hcxsjsd.getMe());
        hcxsjsd.setJe(0 - hcxsjsd.getJe());
        hcxsjsd.setSe(0 - hcxsjsd.getSe());
        if (null != hcxsjsd.getCe())
            hcxsjsd.setCe(0 - hcxsjsd.getCe());
        hcxsjsd.setFpze(0 - hcxsjsd.getFpze());

        List<Xsjsdmx> xsjsdmxList = yxsjsd.getXsjsdmxList();
        List<Xsjsdmx> hcxsjsdmxList = new ArrayList<Xsjsdmx>();
        for (Xsjsdmx xsjsdmx : xsjsdmxList) {
            Xsjsdmx hcxsjsdmx = new Xsjsdmx();
            ReflectionUtils.getInstance(hcxsjsdmx, xsjsdmx);
            hcxsjsdmx.setId(null);
            hcxsjsdmx.setJe(0 - xsjsdmx.getJe());
            hcxsjsdmx.setSe(0 - xsjsdmx.getSe());
//            hcxsjsdmx.setCe(0 - xsjsdmx.getCe());
            hcxsjsdmx.setCksl(0 - xsjsdmx.getCksl());
            if (xsjsdmx.getCbje() != null)
                hcxsjsdmx.setCbje(0 - xsjsdmx.getCbje());
            hcxsjsdmx.setJsd(hcxsjsd);
            hcxsjsdmx.setCreateUser(xsjsdmx.getCreateUser());
            hcxsjsdmx.setCreateOrgan(xsjsdmx.getCreateOrgan());
            hcxsjsdmx.setCreateDepartment(xsjsdmx.getCreateDepartment());
            hcxsjsdmx.setDateCreated(new Date());
            hcxsjsdmx.setLastUpdated(hcxsjsd.getDateCreated());
            this.xsjsdmxDao.save(hcxsjsdmx);
            hcxsjsdmxList.add(hcxsjsdmx);
        }
        hcxsjsd.setXsjsdmxList(hcxsjsdmxList);


        List<Xsmx> xsdmxList = yxsjsd.getXsdmxList();
        for (int i = 0; i < xsdmxList.size(); i++) {
            Xsmx xsmx = xsdmxList.get(i);
            xsmx.setYjs(false);
            xsmx.setXsjsd(null);
            this.xsmxDao.save(xsmx);

            Xsd xsd = xsmx.getXsd();
            if (xsd.getYjs()) {
                Boolean flag = false;
                List<Xsmx> xsmxList = xsmx.getXsd().getXsmxList();
                for (Xsmx xsmx1 : xsmxList) {
                    if (xsmx1.getYjs()) {
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    xsd.setYjs(false);
                    xsd.setXsjsd(null);
                    this.xsdDao.save(xsd);
                }
            }
        }

        hcxsjsd.setSftzd(true);
        hcxsjsd.setYjsd(yxsjsd);
        hcxsjsd.setZjbcdList(null);
        hcxsjsd.setCgdList(null);
        hcxsjsd.setCgdmxList(null);
        hcxsjsd.setXsdList(null);
        hcxsjsd.setXsdmxList(null);
        hcxsjsd.setWldList(null);
        hcxsjsd.setCgjsdmxList(null);
        hcxsjsd.setWljsdmxList(null);
        hcxsjsd.setAcctyear(null);
        hcxsjsd.setUuid(null);
        hcxsjsd.setVoucherId(null);
        hcxsjsd.setVoucherNum(null);
        hcxsjsd.setInsertFlag(1);
        hcxsjsd.setDeleteFlag(1);
        hcxsjsd.setCreateUser(yxsjsd.getCreateUser());
        hcxsjsd.setCreateOrgan(yxsjsd.getCreateOrgan());
        hcxsjsd.setCreateDepartment(yxsjsd.getCreateDepartment());
        hcxsjsd.setDateCreated(new Date());
        hcxsjsd.setLastUpdated(hcxsjsd.getDateCreated());
        hcxsjsd.setSqrq(hcxsjsd.getDateCreated());
        hcxsjsd.setKprq(hcxsjsd.getDateCreated());
        hcxsjsd.setZt(6);
        this.xsjsdDao.save(hcxsjsd);

        yxsjsd.setZjbcdList(null);
        yxsjsd.setCgdList(null);
        yxsjsd.setCgdmxList(null);
        yxsjsd.setXsdList(null);
        yxsjsd.setXsdmxList(null);
        yxsjsd.setWldList(null);
        yxsjsd.setCgjsdmxList(null);
        yxsjsd.setWljsdmxList(null);
        yxsjsd.setSftzd(false);
        yxsjsd.setLastUpdated(new Date());
        yxsjsd.setZt(2);
        this.xsjsdDao.save(yxsjsd);
    }

    /**
     * 关联判断
     *
     * @param xsjsd
     */
    private void nullId(Jsd xsjsd) {
        if (xsjsd != null && xsjsd.getGlqy() != null && xsjsd.getGlqy().getQyfr() != null && xsjsd.getGlqy().getQyfr().getId() == null)
            xsjsd.getGlqy().setQyfr(null);
        if (xsjsd != null && xsjsd.getSqr() != null && xsjsd.getSqr().getId() == null)
            xsjsd.setSqr(null);
        if (xsjsd != null && xsjsd.getGlqy() != null && xsjsd.getGlqy().getId() == null)
            xsjsd.setGlqy(null);
        if (xsjsd != null && xsjsd.getGs() != null && xsjsd.getGs().getId() == null)
            xsjsd.setGs(null);
    }

    public String sumTxx(String xsmxIds) {
        Integer bzjfs = this.xsmxDao.get(Long.valueOf(xsmxIds.split(",")[0])).getXsd().getXsht().getZddhcght().getBzjfs();
        if (null != bzjfs) {
            if (bzjfs.equals(0)) {
                List<Map> list = this.jfmxDao.findSumTxjeFromJfmx(Long.valueOf(xsmxIds.split(",")[0]));
                return list.get(0).get("TXX").toString();
            } else if (bzjfs.equals(1)) {
                Xsmx xsmx = this.xsmxDao.get(Long.valueOf(xsmxIds.split(",")[0]));
                Map<String, String> map = new HashMap<String, String>();
                for (int i = 0; i < xsmxIds.split(",").length; i++) {
                    Xsd xsd = this.xsmxDao.get(Long.valueOf(xsmxIds.split(",")[i])).getXsd();
                    map.put(xsd.getId().toString(), xsd.getId().toString());
                }
                String xsdIds = "";
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    xsdIds += entry.getKey() + ",";

                }
                List<Map> list = this.skshDao.findSumKhTx(xsmx.getXsd().getKh(), xsdIds.substring(0, xsdIds.length() - 1));
                return list.get(0).get("TX").toString();
            } else {
                Xsmx xsmx = this.xsmxDao.get(Long.valueOf(xsmxIds.split(",")[0]));
                List<Map> list = this.hthkDao.findSumHthkTx(xsmx.getXsd().getXsht().getZddhcght());
                return list.get(0).get("TX").toString();
            }
        }
        return "0";
    }

//    public String sumDlf(Long xsmxId) {
//        List<Map> list = this.jfmxDao.findSumDlfjeFromJfmx(xsmxId);
//        return list.get(0).get("DLFJE").toString();
//    }

    /**
     * 计算代理费用
     *
     * @param xsmxId
     * @return
     */
    public Map findSumDlfy(Long xsmxId) {
        Xsd xsd = this.xsmxDao.get(xsmxId).getXsd();
        List<Map> list = this.jfmxDao.findSumTxDlf(xsd.getXsht().getId());
        return list.get(0);
    }

    /**
     * 计算当前贴息和代理费
     *
     * @param xsdIds
     * @return
     */
    public Map findSumTxDlf(String xsdIds) {
        Xsd xsd = this.xsdDao.get(Long.valueOf(xsdIds.split(",")[0]));
        Integer bzjfs = xsd.getXsht().getZddhcght().getBzjfs();
        if (bzjfs != null) {
            if (bzjfs.equals(0)) {
                List<Map> list = this.jfmxDao.findSumTxDlf(xsd.getXsht().getId());
                return list.get(0);
            } else if (bzjfs.equals(1)) {
                List<Map> list = this.skshDao.findSumKhTx(xsd.getKh(), xsdIds.substring(0, xsdIds.length() - 1));
                return list.get(0);
            } else {
                List<Map> list = this.hthkDao.findSumHthkTx(xsd.getXsht().getZddhcght());
                return list.get(0);
            }
        } else {
            Map map = new HashMap();
            map.put("TX", 0);
            map.put("DLF", 0);
            map.put("ZJZYF", 0);
            return map;
        }
    }

    public List findXsjsdByJsdh(String jsdh) {
        String hql = "from Jsd where jsdh =? ";
        return this.xsjsdDao.find(hql, jsdh);
    }

    /**
     * 查找销售结算单明细
     *
     * @param xsjsdmxList
     * @param name
     * @param ggxh
     * @return
     */
    public Xsjsdmx findXsjsdDetailByCondition(List<Xsjsdmx> xsjsdmxList, String name, String ggxh, Double sl, Double bhsj) {
        for (Xsjsdmx temp : xsjsdmxList) {
            Gg wz = temp.getGg();
            String yggxh = wz.getCz() + ' ' + wz.getGgxh();
            Xsmx xsmx = temp.getXsmx();
            if (wz.getName().equals(name) && yggxh.equals(ggxh) && xsmx.getSbsl().equals(sl) && xsmx.getBhsj().equals(bhsj)) {
                return temp;
            }
        }
        return null;
    }

    /**
     * 保存导入结果
     *
     * @param jsdList
     * @param xsjsdmxList
     */
    @Transactional
    public void saveImportInfo(List<Jsd> jsdList, List<Xsjsdmx> xsjsdmxList) {
        for (Jsd jsd : jsdList) {
            this.xsjsdDao.save(jsd);
        }

        for (Xsjsdmx xsjsdmx : xsjsdmxList) {
            this.xsjsdmxDao.save(xsjsdmx);
        }
    }

    //========================get and set==========================

    @Autowired
    public void setJfmxDao(JfmxDao jfmxDao) {
        this.jfmxDao = jfmxDao;
    }

    @Autowired
    public void setXsjsdDao(XsjsdDao xsjsdDao) {
        this.xsjsdDao = xsjsdDao;
    }

    @Autowired
    public void setXsdDao(XsdDao xsdDao) {
        this.xsdDao = xsdDao;
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }

    @Autowired
    public void setXsmxDao(XsmxDao xsmxDao) {
        this.xsmxDao = xsmxDao;
    }

    @Autowired
    public void setXsjsdmxDao(XsjsdmxDao xsjsdmxDao) {
        this.xsjsdmxDao = xsjsdmxDao;
    }

    @Autowired
    public void setXsdService(XsdService xsdService) {
        this.xsdService = xsdService;
    }

    @Autowired
    public void setSkshDao(SkshDao skshDao) {
        this.skshDao = skshDao;
    }

    @Autowired
    public void setHthkDao(HthkDao hthkDao) {
        this.hthkDao = hthkDao;
    }

}
